﻿
using System.ComponentModel.DataAnnotations.Schema;
using System.Data.Entity.ModelConfiguration;
using Sharp.Domain.PlatformBoundedContext.OrgModule.Aggregates;

namespace Sharp.Domain.PlatformBoundedContext.ModelConfigurations
{
    public partial class OrgConfiguration
        : EntityTypeConfiguration<Org>
    {
        public OrgConfiguration()
        { 
            // keys
            HasKey(t => t.Id);

            // Properties
            Property(t => t.Id)
                .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
                .IsRequired();

            Property(t => t.ParentId)
                .IsOptional();
            Property(t => t.OrgLevelId)
                .IsOptional();

            Property(t => t.Code)
                .HasMaxLength(50)
                .IsOptional();

            Property(t => t.Title)
                .HasMaxLength(50)
                .IsRequired();
            Property(t => t.Abbreviation)
                .HasMaxLength(50)
                
                .IsOptional();

            Property(t => t.Remark)
                .HasMaxLength(255)
                .IsOptional();

            Property(t => t.OrgPath)
                .IsMaxLength()
                .IsOptional();

            Property(t => t.OrderIndex)
                .IsRequired();

            Property(t => t.CheckInTime)
                .IsOptional();
            Property(t => t.CheckInUserId)
                .IsOptional();
            Property(t => t.ModifiTime)
                .IsOptional();
            Property(t => t.ModifiedUserId)
                .IsOptional();



            HasOptional(t => t.OrgLevel)
               .WithMany(t => t.Orgs)
               .HasForeignKey(d => d.OrgLevelId);
             
            // Relationships
            HasMany(t => t.AuthorizedUsers)
                .WithMany(t => t.AuthorizedOrgs)
                .Map(m =>
                {
                    m.ToTable("Platform_UserOrgForAuthorized", "dbo");
                    m.MapLeftKey("OrgId");
                    m.MapRightKey("UserId");
                });

            HasMany(t => t.AuthorizedUserGroups)
                .WithMany(t => t.AuthorizedOrgs)
                .Map(m =>
                {
                    m.ToTable("Platform_UserGroupOrgForAuthorized", "dbo");
                    m.MapLeftKey("OrgId");
                    m.MapRightKey("UserGroupId");
                });

            //Map<Department>(x => x.Requires("Discriminator").HasValue("Department"));
            //this.HasRequired(x => x.Department).WithRequiredPrincipal();
        }
    }
}

